import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:jicaimao_flutter/dao/dao_result.dart';
import 'package:jicaimao_flutter/entity/upload_image_entity.dart';
import 'package:jicaimao_flutter/entity/user_status_entity.dart';
import 'package:jicaimao_flutter/net/api.dart';
import 'package:jicaimao_flutter/utils/api_address.dart';
import 'package:jicaimao_flutter/utils/sp_key.dart';
import 'package:jicaimao_flutter/utils/sp_utils.dart';

class UserInfoDao {
  static uploadToServerImage(String filePath) async {
    FormData formData =
        FormData.from({"file": new UploadFileInfo(File(filePath), filePath)});
    var res = await HttpManager.netFetch(NetAddress.uploadToServerImage(),
        formData, null, Options(method: "POST"));
    if (res != null && res.result) {
      var uploadImageModel = UploadImageModel.fromJson(res.data);
      if (uploadImageModel.result.url.isNotEmpty) {
        return DataResult(uploadImageModel.result.url, true);
      }
    } else {
      return DataResult(null, false);
    }
  }

  ///
  ///
  /// 上传用户身份验证信息
  ///
  static uploadToServerIdentifyInfo(String userName, String userIDCardNumber,
      String photo1path, String photo2path, String photo3path) async {
    var useId = SpUtils.getString(SpKey.KEY_USER_ID);
    Map requestParams = {
      "userId": useId,
      "username": userName,
      "identityId": userIDCardNumber,
      "positiveImage": photo1path,
      "oppositeImage": photo2path,
      "holdImage": photo3path
    };

    var res = await HttpManager.netFetch(
        NetAddress.uploadToServerIdentifyInfo(),
        requestParams,
        null,
        Options(
            method: 'POST',
            contentType:
                ContentType.parse("application/x-www-form-urlencoded")));
    if (res != null && res.result) {
      return DataResult(res.data, true);
    } else {
      return DataResult(null, false);
    }
  }

  ///
  /// 获取用户全局状态
  ///
  static getUserSecurityStatus() async {
    var useId = SpUtils.getString(SpKey.KEY_USER_ID);
    Map requestParams = {"userId": useId};
    var res = await HttpManager.netFetch(
        NetAddress.getUserSecurityStatus(),
        requestParams,
        null,
        Options(
            method: 'POST',
            contentType:
                ContentType.parse("application/x-www-form-urlencoded")));
    if (res != null && res.result) {
      Map<String, dynamic> map = json.decode(res.data);
      var userStatusModel = UserStatusModel.fromJson(map);
      return DataResult(userStatusModel.result.auth, true);
    } else {
      return DataResult(null, false);
    }
  }
}
